<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>执行一条语句</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.oci-error.html">« oci_error</a></li>
      <li style="float: right;"><a href="function.oci-fetch-all.html">oci_fetch_all »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.oci8.html">OCI8 函数</a></li>
    <li>执行一条语句</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.oci-execute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">oci_execute</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8, PECL OCI8 &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_execute</span> &mdash; <span class="dc-title">执行一条语句</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.oci-execute-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>oci_execute</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$stmt</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = ?</span></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>oci_execute()</strong></span> 执行一条之前被解析过的语句（见
   <span class="function"><a href="function.oci-parse.html" class="function">oci_parse()</a></span>）。可选参数 <code class="parameter">mode</code>
   允许定义执行模式（默认是
   <strong><code>OCI_COMMIT_ON_SUCCESS</code></strong>）。如果不需要将语句自动提交，则需要把
   <code class="parameter">mode</code> 设为 <strong><code>OCI_DEFAULT</code></strong>。
  </p>
  <p class="para">
   用 <strong><code>OCI_DEFAULT</code></strong>
   模式时，将建立一个事务。事务会在关闭连接或脚本结束时（看哪个先）自动回卷。需要明确调用
   <span class="function"><a href="function.oci-commit.html" class="function">oci_commit()</a></span> 来提交事务，或者
   <span class="function"><a href="function.oci-rollback.html" class="function">oci_rollback()</a></span> 中止事务。
  </p>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 PHP 5.0.0 之前的版本必须使用 <span class="function"><a href="function.ociexecute.html" class="function">ociexecute()</a></span>
    替代本函数。该函数名仍然可用，为向下兼容作为
    <span class="function"><strong>oci_execute()</strong></span> 的别名。不过其已被废弃，不推荐使用。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.oci-execute-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">statement</code></dt>

     <dd>

      <p class="para">
       A valid OCI statement identifier.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
        An optional second parameter can be one of the following constants:
       <table class="doctable table">
        <caption><strong>Execution Modes</strong></caption>
        
         <thead>
          <tr>
           <th>Constant</th>
           <th>Description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><strong><code>OCI_COMMIT_ON_SUCCESS</code></strong></td>
           <td>Automatically commit all outstanding changes for
             this connection when the statement has succeeded. This
             is the default.</td>
          </tr>

          <tr>
           <td><strong><code>OCI_DEFAULT</code></strong></td>
           <td>Obsolete as of PHP 5.3.2 (PECL OCI8 1.4) but still
             available for backward compatibility.  Use the
             equivalent <strong><code>OCI_NO_AUTO_COMMIT</code></strong> in new
             code.</td>
          </tr>

          <tr>
           <td><strong><code>OCI_DESCRIBE_ONLY</code></strong></td>
           <td>Make query meta data available to functions
             like <span class="function"><a href="function.oci-field-name.html" class="function">oci_field_name()</a></span> but do not
             create a result set. Any subsequent fetch call such
             as <span class="function"><a href="function.oci-fetch-array.html" class="function">oci_fetch_array()</a></span> will
             fail.</td>
          </tr>

          <tr>
           <td><strong><code>OCI_NO_AUTO_COMMIT</code></strong></td>
           <td>Do not automatically commit changes.  Prior to PHP
             5.3.2 (PECL OCI8 1.4)
             use <strong><code>OCI_DEFAULT</code></strong> which is an alias
             for <strong><code>OCI_NO_AUTO_COMMIT</code></strong>.</td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <p class="para">
       Using <strong><code>OCI_NO_AUTO_COMMIT</code></strong> mode starts a
       transaction. Transactions are automatically rolled back when
       the connection is closed, or when the script ends.  Explicitly
       call <span class="function"><a href="function.oci-commit.html" class="function">oci_commit()</a></span> to commit a transaction,
       or <span class="function"><a href="function.oci-rollback.html" class="function">oci_rollback()</a></span> to abort it.
      </p>
      <p class="para">
       When inserting or updating data, using transactions is
       recommended for relational data consistency and for performance
       reasons.
      </p>
      <p class="para">
       If <strong><code>OCI_NO_AUTO_COMMIT</code></strong> mode is used for any
       statement including queries, and 
        <span class="function"><a href="function.oci-commit.html" class="function">oci_commit()</a></span>
       or <span class="function"><a href="function.oci-rollback.html" class="function">oci_rollback()</a></span> is not subsequently
       called, then OCI8 will perform a rollback at the end of the
       script even if no data was changed.  To avoid an unnecessary
       rollback, many scripts do not
       use <strong><code>OCI_NO_AUTO_COMMIT</code></strong> mode for queries or
       PL/SQL.  Be careful to ensure the appropriate transactional
       consistency for the application when
       using <span class="function"><strong>oci_execute()</strong></span> with different modes in
       the same script.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.oci-execute-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.oci-execute-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1828">
    <p><strong>示例 #1 <span class="function"><strong>oci_execute()</strong></span> for queries</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;FROM&nbsp;employees'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;table&nbsp;border='1'&gt;\n"</span><span style="color: #007700">;<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">OCI_ASSOC</span><span style="color: #007700">+</span><span style="color: #0000BB">OCI_RETURN_NULLS</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;tr&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$item</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;"&nbsp;</span><span style="color: #007700">.&nbsp;(</span><span style="color: #0000BB">$item&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">)&nbsp;:&nbsp;</span><span style="color: #DD0000">"&amp;nbsp;"</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;/td&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;/table&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1829">
    <p><strong>示例 #2 <span class="function"><strong>oci_execute()</strong></span> without specifying a mode example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;Before&nbsp;running,&nbsp;create&nbsp;the&nbsp;table:<br />//&nbsp;&nbsp;&nbsp;CREATE&nbsp;TABLE&nbsp;MYTABLE&nbsp;(col1&nbsp;NUMBER);<br /><br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'INSERT&nbsp;INTO&nbsp;mytab&nbsp;(col1)&nbsp;VALUES&nbsp;(123)'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;The&nbsp;row&nbsp;is&nbsp;committed&nbsp;and&nbsp;immediately&nbsp;visible&nbsp;to&nbsp;other&nbsp;users<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1830">
    <p><strong>示例 #3 <span class="function"><strong>oci_execute()</strong></span> with <strong><code>OCI_NO_AUTO_COMMIT</code></strong> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;Before&nbsp;running,&nbsp;create&nbsp;the&nbsp;table:<br />//&nbsp;&nbsp;&nbsp;CREATE&nbsp;TABLE&nbsp;MYTABLE&nbsp;(col1&nbsp;NUMBER);<br /><br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'INSERT&nbsp;INTO&nbsp;mytab&nbsp;(col1)&nbsp;VALUES&nbsp;(:bv)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_bind_by_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">':bv'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;use&nbsp;OCI_DEFAULT&nbsp;for&nbsp;PHP&nbsp;&lt;=&nbsp;5.3.1<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">oci_commit</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;commits&nbsp;all&nbsp;new&nbsp;values:&nbsp;1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1831">
    <p><strong>示例 #4 <span class="function"><strong>oci_execute()</strong></span> with different commit modes example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;Before&nbsp;running,&nbsp;create&nbsp;the&nbsp;table:<br />//&nbsp;&nbsp;&nbsp;CREATE&nbsp;TABLE&nbsp;MYTABLE&nbsp;(col1&nbsp;NUMBER);<br /><br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'INSERT&nbsp;INTO&nbsp;mytab&nbsp;(col1)&nbsp;VALUES&nbsp;(123)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;data&nbsp;not&nbsp;committed<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'INSERT&nbsp;INTO&nbsp;mytab&nbsp;(col1)&nbsp;VALUES&nbsp;(456)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;commits&nbsp;both&nbsp;123&nbsp;and&nbsp;456&nbsp;values<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1832">
    <p><strong>示例 #5 <span class="function"><strong>oci_execute()</strong></span> with
     <strong><code>OCI_DESCRIBE_ONLY</code></strong> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;FROM&nbsp;locations'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">OCI_DESCRIBE_ONLY</span><span style="color: #007700">);<br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">oci_num_fields</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);&nbsp;++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">oci_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.oci-execute-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    Transactions are automatically rolled back when connections are
    closed, or when the script ends, whichever is soonest.  Explicitly
    call <span class="function"><a href="function.oci-commit.html" class="function">oci_commit()</a></span> to commit a transaction.
   </p>
   <p class="para">
    Any call to <span class="function"><strong>oci_execute()</strong></span> that uses
    <strong><code>OCI_COMMIT_ON_SUCCESS</code></strong> mode explicitly or by
    default will commit any previous uncommitted transaction.
   </p>
   <p class="para">
    Any Oracle DDL statement such as <code class="literal">CREATE</code>
    or <code class="literal">DROP</code> will automatically commit any
    uncommitted transaction.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    Because the <span class="function"><strong>oci_execute()</strong></span> function generally
    sends the statement to the
    database, <span class="function"><strong>oci_execute()</strong></span> can identify some
    statement syntax errors that the lightweight,
    local <span class="function"><a href="function.oci-parse.html" class="function">oci_parse()</a></span> function does not.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    In PHP versions before 5.0.0 use <span class="function"><a href="function.ociexecute.html" class="function">ociexecute()</a></span>
    instead.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.oci-execute-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.oci-parse.html" class="function" rel="rdfs-seeAlso">oci_parse()</a> - 配置 Oracle 语句预备执行</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>